define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init();

            //绑定事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var panel = $($(this).attr("href"));
                if (panel.length > 0) {
                    Controller.table[panel.attr("id")].call(this);
                    $(this).on('click', function (e) {
                        $($(this).attr("href")).find(".btn-refresh").trigger("click");
                    });
                }
                //移除绑定的事件
                $(this).unbind('shown.bs.tab');
            });

            //必须默认触发shown.bs.tab事件
            $('ul.nav-tabs li.active a[data-toggle="tab"]').trigger("shown.bs.tab");
        },
        table: {
            subject: function () {

                var SubjectTable = $("#SubjectTable");

                Table.api.init({
                    extend: {
                        index_url: 'subject/subject/recyclebin',
                        restore_url: 'subject/subject/restore',
                        destroy_url: 'subject/subject/destroy',
                        table: 'subject',
                    }
                });

                SubjectTable.bootstrapTable({
                    url: $.fn.bootstrapTable.defaults.extend.index_url,
                    toolbar: '#SubjectToolbar',
                    pk: 'id',
                    sortName: 'createtime',
                    sortOrder: 'desc',
                    columns: [
                        [
                            { checkbox: true },
                            { field: 'id', title: __('Id'), sortable: true, operate: false },
                            { field: 'title', title: __('Stitle'), operate: 'LIKE' },
                            { field: 'category.name', title: __('Cname') },
                            { field: 'thumbs_text', title: __('Image'), formatter: Table.api.formatter.image, operate: false },
                            { field: 'price', title: __('Sprice'), sortable: true, operate: false },
                            { field: 'likes_text', title: __('Slikes'), operate: false },
                            { field: 'createtime', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime },
                            { field: 'deletetime', title: __('Deletetime'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime },
                            {
                                field: 'operate', title: __('Operate'), table: SubjectTable, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                                buttons: [
                                    {
                                        name: 'restore',
                                        title: '还原',
                                        classname: 'btn btn-xs btn-success btn-ajax',
                                        icon: 'fa fa-circle-o-notch',
                                        url: $.fn.bootstrapTable.defaults.extend.restore_url,
                                        extend: 'data-toggle=\'tooltip\'',
                                        confirm: '确认是否确认还原数据',
                                        refresh: true, //操作完成后是否刷新列表
                                    },
                                    {
                                        name: 'destroy',
                                        title: '销毁',
                                        classname: 'btn btn-xs btn-danger btn-ajax',
                                        icon: 'fa fa-trash',
                                        url: $.fn.bootstrapTable.defaults.extend.destroy_url,
                                        extend: 'data-toggle=\'tooltip\'',
                                        confirm: '是否确认真实销毁数据',
                                        refresh: true,
                                    }
                                ]
                            }
                        ]
                    ]
                });

                // 还原
                $(".btn-restore").click(function () {

                    //获取选中的条目ID集合
                    let ids = Table.api.selectedids(SubjectTable)

                    layer.confirm('确认是否还原数据?', {
                        title: '还原提醒',
                        btn: ['确定', '取消']
                    }, function (index) {
                        Fast.api.ajax({
                            url: $.fn.bootstrapTable.defaults.extend.restore_url + `?ids=${ids}`
                        }, function () {
                            // 关闭弹窗
                            layer.close(index);

                            //刷新表格
                            SubjectTable.bootstrapTable('refresh')
                        })
                    });
                })

                // 销毁
                $(".btn-destroy").click(function () {

                    //获取选中的条目ID集合
                    let ids = Table.api.selectedids(SubjectTable)

                    layer.confirm('是否确认真实销毁数据?', {
                        title: '删除提醒',
                        btn: ['确定', '取消']
                    }, function (index) {
                        Fast.api.ajax({
                            url: $.fn.bootstrapTable.defaults.extend.destroy_url + `?ids=${ids}`
                        }, function () {
                            // 关闭弹窗
                            layer.close(index);

                            //刷新表格
                            SubjectTable.bootstrapTable('refresh')
                        })
                    });
                })

                Table.api.bindevent(SubjectTable);
            },
            order: function () {

                // 订单
                var OrderTable = $("#OrderTable");

                Table.api.init({
                    extend: {
                        index_url: 'subject/order/recyclebin',
                        restore_url: 'subject/order/restore',
                        destroy_url: 'subject/order/destroy',
                        table: 'subject_order',
                    }
                });

                OrderTable.bootstrapTable({
                    url: $.fn.bootstrapTable.defaults.extend.index_url,
                    toolbar: '#OrderToolbar',
                    pk: 'id',
                    sortName: 'createtime',
                    sortOrder: 'desc',
                    columns: [
                        [
                            { checkbox: true },
                            { field: 'id', title: __('Id'), sortable: true, operate: false },
                            { field: 'code', title: __('Ocode'), operate: 'LIKE' },
                            { field: 'subject.title', title: __('Otitle'), operate: 'LIKE' },
                            { field: 'business.nickname', title: __('Bnickname'), operate: 'LIKE' },
                            { field: 'total', title: __('Ototal'), sortable: true, operate: 'LIKE' },
                            { field: 'createtime', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime },
                            { field: 'deletetime', title: __('Deletetime'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime },
                            {
                                field: 'operate', title: __('Operate'), table: OrderTable, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                                buttons: [
                                    {
                                        name: 'restore',
                                        title: '还原',
                                        classname: 'btn btn-xs btn-success btn-ajax',
                                        icon: 'fa fa-circle-o-notch',
                                        url: $.fn.bootstrapTable.defaults.extend.restore_url,
                                        extend: 'data-toggle=\'tooltip\'',
                                        confirm: '确认是否确认还原数据',
                                        refresh: true,
                                    },
                                    {
                                        name: 'destroy',
                                        title: '销毁',
                                        classname: 'btn btn-xs btn-danger btn-ajax',
                                        icon: 'fa fa-trash',
                                        url: $.fn.bootstrapTable.defaults.extend.destroy_url,
                                        extend: 'data-toggle=\'tooltip\'',
                                        confirm: '是否确认真实销毁数据',
                                        refresh: true,
                                    }
                                ]
                            }
                        ]
                    ]
                });

                // 还原
                $(".btn-restore").click(function () {

                    //获取选中的条目ID集合
                    let ids = Table.api.selectedids(OrderTable)

                    layer.confirm('确认是否还原数据?', {
                        title: '还原提醒',
                        btn: ['确定', '取消']
                    }, function (index) {
                        Fast.api.ajax({
                            url: $.fn.bootstrapTable.defaults.extend.restore_url + `?ids=${ids}`
                        }, function () {
                            // 关闭弹窗
                            layer.close(index);

                            //刷新表格
                            OrderTable.bootstrapTable('refresh')
                        })
                    });
                })

                // 销毁
                $(".btn-destroy").click(function () {

                    //获取选中的条目ID集合
                    let ids = Table.api.selectedids(OrderTable)

                    layer.confirm('是否确认真实销毁数据?', {
                        title: '删除提醒',
                        btn: ['确定', '取消']
                    }, function (index) {
                        Fast.api.ajax({
                            url: $.fn.bootstrapTable.defaults.extend.destroy_url + `?ids=${ids}`
                        }, function () {
                            // 关闭弹窗
                            layer.close(index);

                            //刷新表格
                            OrderTable.bootstrapTable('refresh')
                        })
                    });
                })

                // 为表格1绑定事件
                Table.api.bindevent(OrderTable);
            },
            pay: function () {

                var PayTable = $("#PayTable");

                Table.api.init({
                    extend: {
                        index_url: 'pay/pay/recyclebin',
                        restore_url: 'pay/pay/restore',
                        destroy_url: 'pay/pay/destroy',
                        table: 'pay',
                    }
                });

                PayTable.bootstrapTable({
                    url: $.fn.bootstrapTable.defaults.extend.index_url,
                    toolbar: '#PayToolbar',
                    pk: 'id',
                    sortName: 'createtime',
                    sortOrder: 'desc',
                    fixedColumns: true,
                    fixedRightColumn: 1,
                    columns: [
                        [
                            { checkbox: true },
                            { field: 'id', title: __('Id'), sortable: true, operate: false },
                            { field: 'code', title: __('Pcode'), operate: 'LIKE' },
                            { field: 'name', title: __('Pname'), operate: 'LIKE' },
                            {
                                field: 'type', title: __('Ptype'),
                                searchList: {
                                    "wx": __('微信支付'),
                                    "zfb": __('支付宝支付')
                                },
                                operate: 'FIND_IN_SET',
                                formatter: Table.api.formatter.normal,
                                custom: { "zfb": "success" }
                            },
                            { field: 'total', title: __('Ptotal'), operate: 'RANGE' },
                            { field: 'price', title: __('Pprice'), operate: 'RANGE' },
                            { field: 'remarks', title: __('Premarks'), operate: 'LIKE' },
                            { field: 'jump', title: __('Pjump'), operate: 'LIKE', formatter: Table.api.formatter.url },
                            { field: 'notice', title: __('Pnotice'), operate: 'LIKE', formatter: Table.api.formatter.url },
                            {
                                field: 'status', title: __('Pstatus'),
                                searchList: {
                                    "0": __('待支付'),
                                    "1": __('已支付'),
                                    "2": __("已关闭")
                                },
                                operate: 'FIND_IN_SET',
                                formatter: Table.api.formatter.flag,
                                custom: { "0": "warning", "1": "success", "2": "danger" }
                            },
                            { field: 'wxcode', title: __('Pwxcode'), formatter: Table.api.formatter.image, operate: false },
                            { field: 'zfbcode', title: __('Pzfbcode'), formatter: Table.api.formatter.image, operate: false },
                            { field: 'createtime', title: __('Pcreatetime'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime },
                            { field: 'paytime', title: __('Ppaytime'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime },
                            {
                                field: 'operate', title: __('Operate'), table: PayTable, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                                buttons: [
                                    {
                                        name: 'restore',
                                        title: '还原',
                                        classname: 'btn btn-xs btn-success btn-ajax',
                                        icon: 'fa fa-circle-o-notch',
                                        url: $.fn.bootstrapTable.defaults.extend.restore_url,
                                        extend: 'data-toggle=\'tooltip\'',
                                        confirm: '确认是否确认还原数据',
                                        refresh: true, //操作完成后是否刷新列表
                                    },
                                    {
                                        name: 'destroy',
                                        title: '销毁',
                                        classname: 'btn btn-xs btn-danger btn-ajax',
                                        icon: 'fa fa-trash',
                                        url: $.fn.bootstrapTable.defaults.extend.destroy_url,
                                        extend: 'data-toggle=\'tooltip\'',
                                        confirm: '是否确认真实销毁数据',
                                        refresh: true,
                                    }
                                ]
                            }
                        ]
                    ]
                });

                // 还原
                $(".btn-restore").click(function () {

                    //获取选中的条目ID集合
                    let ids = Table.api.selectedids(PayTable)

                    layer.confirm('确认是否还原数据?', {
                        title: '还原提醒',
                        btn: ['确定', '取消']
                    }, function (index) {
                        Fast.api.ajax({
                            url: $.fn.bootstrapTable.defaults.extend.restore_url + `?ids=${ids}`
                        }, function () {
                            // 关闭弹窗
                            layer.close(index);

                            //刷新表格
                            PayTable.bootstrapTable('refresh')
                        })
                    });
                })

                // 销毁
                $(".btn-destroy").click(function () {

                    //获取选中的条目ID集合
                    let ids = Table.api.selectedids(PayTable)

                    layer.confirm('是否确认真实销毁数据?', {
                        title: '删除提醒',
                        btn: ['确定', '取消']
                    }, function (index) {
                        Fast.api.ajax({
                            url: $.fn.bootstrapTable.defaults.extend.destroy_url + `?ids=${ids}`
                        }, function () {
                            // 关闭弹窗
                            layer.close(index);

                            //刷新表格
                            PayTable.bootstrapTable('refresh')
                        })
                    });
                })

                Table.api.bindevent(PayTable);
            }
        },
        restore: function () {
            Controller.api.bindevent();
        },
        destroy: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
        }
    };
    return Controller;
});
